package com.example.travel.controller;

import com.example.travel.bean.ActivityBean;
import com.example.travel.bean.FeedbackBean;
import com.example.travel.service.ActivityService;
import com.example.travel.service.FeedbackService;
import com.example.travel.utils.DateFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class FeedbackController {

    @Autowired
    FeedbackService feedbackService;

    @RequestMapping("/getFeedbackList")
    public ModelAndView getFeedbackList(@RequestParam("page") int page, @RequestParam("size")  int size,@RequestParam("keyword")  String keyword) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        List<FeedbackBean> dataList;
        if(keyword == null || keyword.isEmpty()){
            dataList = feedbackService.getFeedbackList(page, size);
        }else{
            dataList = feedbackService.getFeedbackListKeyword(page, size, "%"+keyword+"%");
        }
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", "success");
        modelAndView.addObject("total", dataList.size());
        return modelAndView;
    }

    @RequestMapping(value="/addFeedback", method=RequestMethod.POST)
    public ModelAndView addFeedback(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        String feedbackUser = params.get("feedbackUser").toString();
        String feedbackType = params.get("feedbackType").toString();
        String feedbackText = params.get("feedbackText").toString();

        FeedbackBean feedbackBean = new FeedbackBean();
        feedbackBean.setFeedbackUser(feedbackUser);
        feedbackBean.setFeedbackType(feedbackType);
        feedbackBean.setFeedbackText(feedbackText);

        DateFormatter dateFormatter = new DateFormatter();
        String feedbackDate = dateFormatter.getDate();//获取当前日期作为反馈时间
        feedbackBean.setFeedbackDate(feedbackDate);

        int status = feedbackService.addFeedback(feedbackBean);
        modelAndView.addObject("dataList", new ArrayList<>());
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    // 新增：获取所有反馈内容用于生成词云
    @RequestMapping(value="/getAllFeedbackForWordCloud", method=RequestMethod.GET)
    public ModelAndView getAllFeedbackForWordCloud() {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        try {
            String allFeedbackText = feedbackService.getAllFeedbackTextForWordCloud();
            modelAndView.addObject("status", "success");
            modelAndView.addObject("data", allFeedbackText);
            modelAndView.addObject("message", "获取反馈内容成功");
        } catch (Exception e) {
            modelAndView.addObject("status", "error");
            modelAndView.addObject("message", "获取反馈内容失败: " + e.getMessage());
        }
        return modelAndView;
    }

}

